package com.dailyblue.java.spring.boot.redis.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@RequestMapping("/set")
@RestController
@Slf4j
public class SetController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/a")
    public String a() {
        SetOperations<String, String> set = stringRedisTemplate.opsForSet();
        set.add("A", "aa", "bb", "cc", "11", "22", "33", "44", "55");
        set.add("B","aa","bb","cc","dd","ee");
        set.add("C","bb","22","33","ee","44");

        return "";
    }

    @GetMapping("/b")
    public String b() {
        SetOperations<String, String> set = stringRedisTemplate.opsForSet();
        Set<String> all = set.members("A");
        String v1 = set.randomMember("A");
        List<String> list = set.randomMembers("A",4);
        Set<String> set1 = set.distinctRandomMembers("A",4);
        log.info("{},{},{},{}",all,v1,list,set1);
        return "";
    }

    @GetMapping("/c")
    public String c() {
        SetOperations<String, String> set = stringRedisTemplate.opsForSet();
        Set<String> set1 = set.difference("B", "C");
        Set<String> set2 = set.union("B", "C");
        Set<String> set3 = set.intersect("B", "C");
        log.info("{}",set1);
        log.info("{}",set2);
        log.info("{}",set3);

        return "";
    }
}
